import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class HelloOpenCV {
    public void run() {
        //资源路径不能包含中文,否则openCV识别不了
        String resourcePath="C:/tmp/data";
        String facePath = "";
        System.out.println("\nRunning DetectFaceDemo");

        String xmlResourcePath ="";
        String outFilename ="";

        //lbpcascade_frontalface_improved 没有显示整张脸

        //lena.png 配合 lbpcascade_frontalface.xml 识别不出来
        xmlResourcePath= resourcePath + "/lbpcascade_frontalface.xml";
        facePath=resourcePath+"/lena.png";
        outFilename = resourcePath+"/faceDetection1.png";
        detectedFace(xmlResourcePath,facePath,outFilename);

        //lena.png 能够识别
        xmlResourcePath= resourcePath + "/lbpcascade_frontalface_improved.xml";
        facePath=resourcePath+"/lena.png";
        outFilename = resourcePath+"/faceDetection2.png";
        detectedFace(xmlResourcePath,facePath,outFilename);


        //face1.png 可以识别出来人脸
        xmlResourcePath= resourcePath + "/lbpcascade_frontalface.xml";
        facePath=resourcePath+"/face1.png";
        outFilename = resourcePath+"/faceDetection3.png";
        detectedFace(xmlResourcePath,facePath,outFilename);

        //face1.png 可以识别出来人脸
        xmlResourcePath= resourcePath + "/lbpcascade_frontalface_improved.xml";
        facePath=resourcePath+"/face1.png";
        outFilename = resourcePath+"/faceDetection4.png";
        detectedFace(xmlResourcePath,facePath,outFilename);

    }

    public void detectedFace(String xmlResourcePath,String facePath,String outFilename){

        // Create a face detector from the cascade file in the resources
        // directory.
        CascadeClassifier faceDetector = new CascadeClassifier(xmlResourcePath);
        System.out.println("lenaPath="+facePath);
        Mat image = Imgcodecs.imread(facePath);

        // Detect faces in the image.
        // MatOfRect is a special container class for Rect.
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

        // Draw a bounding box around each face.
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
        }

        // Save the visualized detection.
        System.out.println(String.format("Writing %s", outFilename));
        Imgcodecs.imwrite(outFilename, image);
    }

    public static void main(String[] args) {
        System.out.println("Hello, OpenCV");

        // Load the native library.
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        new HelloOpenCV().run();
    }
}
